/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wat.serverapp.remote;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import wat.ziszcommon.remote.ClassProvider;
import wat.ziszcommon.remote.CryptoUtils;

/**
 *
 * @author Marcin
 */
public class ClassProviderImpl implements ClassProvider {

    private String dirPath;

    public ClassProviderImpl() {
        this.dirPath = "c:/repo/";
    }

    public byte[] getClass(String className) {
        FileInputStream fis = null;
        try {
            String path = className.replace('.', '/').concat(".class");
            File file = new File(dirPath+path);
            byte[] data = new byte[(int) file.length()];
            fis = new FileInputStream(file);
            if (fis.read(data) == data.length) {
                return data;
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JarProviderImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JarProviderImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(JarProviderImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }

    public String getMd5(String className) {
        FileInputStream fis = null;
        try {
            String path = className.replace('.', '/').concat(".class");
            File file = new File(dirPath+path);
            byte[] data = new byte[(int) file.length()];
            fis = new FileInputStream(file);
            if (fis.read(data) == data.length) {
                return CryptoUtils.md5(data);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(JarProviderImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(JarProviderImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                fis.close();
            } catch (IOException ex) {
                Logger.getLogger(JarProviderImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }
}
